﻿@{ Layout = "~/Views/Shared/_ModalForm.cshtml";
    string controller = ViewBag.Controller; }

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <input id="type" col="Type" type="text" class="hidden" />
        <div class="form-group">
            <label class="col-sm-3 control-label ">名称<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="name" col="Name" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">排序码</label>
            <div class="col-sm-8">
                <input id="sequence" col="Sequence" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">状态</label>
            <div class="col-sm-8" id="status" col="Status">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">描述</label>
            <div class="col-sm-8">
                <textarea id="description" col="Description" class="form-control" style="height:68px"></textarea>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var type = ys.request("type");
     var act = 'edit';
    if (id == '' | id == null) act = 'add';


    $(function () {
        $("#status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });
        getForm();

        $("#form").validate({
            rules: {
                name: { required: true, maxlength: 63 },
                sequence: { digits: true },
            }
        });
    });

    function getForm() {
        if (act == 'add') {//add
            ys.ajax({
                url: '@Url.Content("~/Sys/" + "UserGroup" + "/GetMaxSequenceNoJson")'+'?type='+type,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var defaultData = {};
                        defaultData.Sequence = rst.Data;
                        defaultData.Type = type;
                        defaultData.Status = "@((int)StatusType.Enabled)";
                        $("#form").setWebControls(defaultData);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }
        else {//edit
            ys.ajax({
                url: '@Url.Content("~/Sys/"+ "UserGroup" + "/GetEditDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        //rst.Data.Type = type;
                        $("#form").setWebControls(rst.Data);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }

    }

    function saveForm(index) {
        if (!ys.checkTextLenth("description", "描述", 0, 127)) return;

        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            //postData.MenuItemIds = $('#menuTree').ysTree("getCheckedNodes");
            ys.ajax({
                url: '@Url.Content("~/Sys/" + "UserGroup" + "/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag == 1) {
                        ys.msgSuccess(rst.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(rst.Message);
                    }
                }
            });
        }
    }


</script>
